Imports DotNetNuke.Framework.Providers
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Common.Utilities.Config
Imports System.StringComparer
Imports System.Data
Imports System.Data.SqlClient

Public Class FeatureGroupController
#Region "Methods"
    Public Sub New()
        Dim provider As DotNetNuke.Framework.Providers.Provider = CType(Me._providerConfiguration.Providers.Item(Me._providerConfiguration.DefaultProvider), Provider)
        Me._objectQualifier = provider.Attributes.Item("objectQualifier")
        If (Me._objectQualifier <> String.Empty And Not Me._objectQualifier.EndsWith("_")) Then
            Me._objectQualifier = (Me._objectQualifier & "_")
        End If
        Me._databaseOwner = provider.Attributes.Item("databaseOwner")
        If (Me._databaseOwner <> String.Empty And Not Me._databaseOwner.EndsWith(".")) Then
            Me._databaseOwner = (Me._databaseOwner & ".")
        End If
    End Sub

    Public Sub DeleteFeatureGroup(ByVal FeatureGroupId As Integer)
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_FeatureGroup_Delete", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@FeatureGroupId", SqlDbType.Int).Value = FeatureGroupId
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
        Catch ex As Exception
            
        End Try
    End Sub

    Public Function GetFeatureGroup(ByVal FeatureGroupId As Integer) As FeatureGroupInfo
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_FeatureGroup_GetByFeatureGroupId", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@FeatureGroupId", SqlDbType.Int).Value = FeatureGroupId
            connection.Open()
            Dim reader As IDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
            Dim fgi As New FeatureGroupInfo
            Do While reader.Read
                fgi.FeatureGroup = Convert.ToString((reader.Item("FeatureGroup")))
                fgi.FeatureGroupId = FeatureGroupId
                fgi.ProductGroupId = CInt(Convert.ToUInt32((reader.Item("ProductGroupId"))))
                fgi.SortOrder = CInt(Convert.ToUInt32((reader.Item("SortOrder"))))
            Loop
            reader.Close()
            Return fgi
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function GetFeatureGroupsByProductGroupId(ByVal ProductGroupId As Integer) As IDataReader
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_FeatureGroup_GetByProductGroupId", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@ProductGroupId", SqlDbType.Int).Value = ProductGroupId
            connection.Open()
            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Function GetNull(ByVal Field As Object) As Object
        Return Null.GetNull((Field), DBNull.Value)
    End Function

    Public Function SaveFeatureGroup(ByVal fg As FeatureGroupInfo) As Integer
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_FeatureGroup_Save", connection)
            command.CommandType = CommandType.StoredProcedure
            Dim parameters As SqlParameterCollection = command.Parameters
            parameters.Add("@FeatureGroupId", SqlDbType.Int).Value = fg.FeatureGroupId
            parameters.Add("@FeatureGroup", SqlDbType.NVarChar, &HFF).Value = fg.FeatureGroup
            parameters.Add("@ProductGroupId", SqlDbType.Int).Value = fg.ProductGroupId
            parameters.Add("@SortOrder", SqlDbType.Int).Value = fg.SortOrder
            parameters = Nothing
            connection.Open()
            fg.FeatureGroupId = CInt(command.ExecuteScalar)
            connection.Close()
            command.Dispose()
            connection.Dispose()
            Return fg.FeatureGroupId
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function SaveFeatureGroupSortOrder(ByVal FeatureGroupId As Integer, ByVal NewPosition As Integer, ByVal OldPosition As Integer) As Boolean
        Try
            Dim connection As New SqlConnection(Me.ConnectionString)
            Dim command As New SqlCommand(DatabaseOwner & ObjectQualifier & "activeapc_dnn_FeatureGroup_SaveSortOrder", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@FeatureGroupId", SqlDbType.Int).Value = FeatureGroupId
            command.Parameters.Add("@SortOrder", SqlDbType.Int).Value = NewPosition
            command.Parameters.Add("@OldSortOrder", SqlDbType.Int).Value = OldPosition
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
#End Region

#Region "Public Properties"
    Public ReadOnly Property ConnectionString() As String
        Get
            Return Me._connectionString
        End Get
    End Property

    Public ReadOnly Property DatabaseOwner() As String
        Get
            Return Me._databaseOwner
        End Get
    End Property

    Public ReadOnly Property ObjectQualifier() As String
        Get
            Return Me._objectQualifier
        End Get
    End Property
#End Region

#Region "Private Members"
    Private _connectionString As String = DotNetNuke.Common.Utilities.Config.GetConnectionString
    Private _databaseOwner As String
    Private _objectQualifier As String
    Private _providerConfiguration As ProviderConfiguration = ProviderConfiguration.GetProviderConfiguration("data")
    Private Const ProviderType As String = "data"
#End Region
End Class

 
